* Jawbone.do
* 2016.05.07
* Processes Jawbone data and estimates LR 1st stage
* Data from https://jawbone.com/blog/circadian-rhythm/, downloaded 5/7/2016.

capture log close
set more off
timer clear 1
timer on 1
clear
set matsize 10000
local work "/DIRECTORY"
log using "`work'/logs/Jawbone.log", replace
eststo clear
graph set window fontface "CMU Serif"
set scheme jleanc

* Reading sleep data from JS file
insheet using "`work'/data/jawbone/counties.prod_manualproc.txt", comma names clear

* Constructing state-county fips codes
generate state_code = substr(code, 4, 2)
replace state_code = upper(state_code)
merge m:1 state_code using "`work'/data/state_latlon_fips.dta", keepusing(gestfips) keep(3) nogenerate /* merge drops St. Thomas, US VI */
generate county_fips = substr(code, 7, 3)
generate fips = gestfips + county_fips

* Merging county latitudes and longitudes
merge 1:1 fips using "`work'/data/county_tz/county_latlong.dta", keepusing(latitude longitude) keep(1 3) nogenerate

* Merging county TZ offsets
merge 1:1 fips using "`work'/data/county_tz/county_time_zone.dta", keepusing(tz_offset time_zone) keep(1 3) nogenerate

* Computing annual average sunset
* Jawbone blog post (data source) does not say when data collected. Published October 2014.
* This code block adapted from "annual_atus_sunset.do"
generate year = 2013
expand 365
bysort fips: gen doy=_n
encode fips, generate(id)
gen date = mdy(1,1,year) + doy - 1
format date %td
// Getting time zone offset right
gen observe_dst = 1
replace observe_dst = 0 if time_zone == "e" | time_zone == "h" | time_zone == "m"
gen dst = 0
rename tz_offset tz_offset_nodst
replace dst = 1 if observe_dst == 1 & date >= td(6apr2003) & date < td(26oct2003)
replace dst = 1 if observe_dst == 1 & date >= td(4apr2004) & date < td(31oct2004)
replace dst = 1 if observe_dst == 1 & date >= td(3apr2005) & date < td(30oct2005)
replace dst = 1 if observe_dst == 1 & date >= td(2apr2006) & date < td(29oct2006)
replace dst = 1 if observe_dst == 1 & date >= td(11mar2007) & date < td(4nov2007)
replace dst = 1 if observe_dst == 1 & date >= td(9mar2008) & date < td(2nov2008)
replace dst = 1 if observe_dst == 1 & date >= td(8mar2009) & date < td(1nov2009)
replace dst = 1 if observe_dst == 1 & date >= td(14mar2010) & date < td(7nov2010)
replace dst = 1 if observe_dst == 1 & date >= td(13mar2011) & date < td(6nov2011)
replace dst = 1 if observe_dst == 1 & date >= td(11mar2012) & date < td(4nov2012)
replace dst = 1 if observe_dst == 1 & date >= td(10mar2013) & date < td(3nov2013)
replace dst = 1 if observe_dst == 1 & date >= td(9mar2014) & date < td(2nov2014)
replace dst = 1 if observe_dst == 1 & date >= td(8mar2015) & date < td(1nov2015)
gen tz_offset = tz_offset_nodst + dst
* Solar calculator
solar_calculator latitude longitude date tz_offset
drop sun_ra sun_dec solar_noon
rename sunset_time sunset_time_avg
rename sunrise_time sunrise_time_avg
rename sunlight_duration sunlight_duration_avg
* Collapsing to xsec in counties
collapse (mean) waketime total_hrs bedtime sunset_time_avg sunrise_time_avg sunlight_duration_avg, by(id fips state_code latitude longitude time_zone)
* End block adapted from "annual_atus_sunset.do"

* Converting sleep to hours per week, so matches ATUS
replace total_hrs=total_hrs*7

* Merging control variables
merge m:1 fips using "`work'/data/demographics/CountiesDB_subset.dta", keep(1 3 4 5) keepusing(pop_density pop_2010 mean_age_2010 pct_female_2010 occ_* pct_black_2010 pct_asian_2010 pct_white_2010 pct_other_2010) update nogenerate
merge m:1 fips using "`work'/data/coastal_counties.dta", keep(1 3)
gen coastal = (_merge == 3)
drop _merge
merge m:1 fips using "`work'/data/gis/Stata inputs/CntyCoastDistances.dta", keep(1 3) nogenerate

* Control variable handling
rename mean_age_2010 mean_age
gen age2 = mean_age*mean_age
mkspline lat_10_ 10 = latitude
mkspline pd_ 5 = pop_density
mkspline cd_ 3 = coast_dist
gen coast = ((coastal == 1 & coast_dist < 1))
gen cc = coast_dist*coast

* Subset to continental US
keep if inlist(time_zone, "C", "E", "M", "P")
encode time_zone, generate(tzcode)

* Models
* As in QCEW analysis, excluding "donuts" at TZ boundaries
label variable sunset_time_avg "Avg. sunset time"
local donutPM "-117.38, -114.74"
local donutMC "-104.58, -100.58"
local donutCE "-89.28, -84.93"
local geographic = "coast_dist"
local demographic = "mean_age pct_female_2010 pct_black_2010 pct_white_2010"
eststo clear
eststo: regress bedtime sunset_time_avg if !inrange(longitude, `donutPM') & !inrange(longitude, `donutMC') & !inrange(longitude, `donutCE'), vce(robust)
eststo: regress waketime sunset_time_avg if !inrange(longitude, `donutPM') & !inrange(longitude, `donutMC') & !inrange(longitude, `donutCE'), vce(robust)
eststo: regress total_hrs sunset_time_avg if !inrange(longitude, `donutPM') & !inrange(longitude, `donutMC') & !inrange(longitude, `donutCE'), vce(robust)
esttab using "`work'/tables/xsec/Jawbone_bedwakesleep.tex", ///
			drop(_cons) ///
			star(* 0.10 ** 0.05 *** 0.01) ///
			mlabels("Bedtime" "Wake time" "Sleep") ///
			ar2 se noconstant nonumbers nogaps label tex fragment replace b(a2) se(a2)

eststo clear
eststo: regress total_hrs i.tzcode `geographic' sunset_time_avg if !inrange(longitude, `donutPM') & !inrange(longitude, `donutMC') & !inrange(longitude, `donutCE'), vce(robust)
eststo: regress total_hrs i.tzcode `geographic' `demographic' sunset_time_avg if !inrange(longitude, `donutPM') & !inrange(longitude, `donutMC') & !inrange(longitude, `donutCE'), vce(robust)
esttab using "`work'/tables/xsec/Jawbone_firststage.tex", ///
			indicate("TZ dummies=*.tzcode" "Coastal distance=`geographic'" "Demographic controls=`demographic'") ///
			drop(_cons) ///
			star(* 0.10 ** 0.05 *** 0.01) ///
			ar2 se noconstant nonumbers nogaps label tex fragment replace b(a2) se(a2)





* Ending commands
timer off 1
timer list 1
capture log close


